﻿Imports Microsoft.VisualStudio.TestTools.UnitTesting

Imports QBuilder



'''<summary>
'''This is a test class for InCriteriaTest and is intended
'''to contain all InCriteriaTest Unit Tests
'''</summary>
<TestClass()> _
Public Class QueryOptionsTest


    Private testContextInstance As TestContext

    Public Property TestContext() As TestContext
        Get
            Return testContextInstance
        End Get
        Set(value As TestContext)
            testContextInstance = value
        End Set
    End Property

    ''' <summary>
    ''' This test makes sure that non-default query options are being properly pushed to all the sub-parts of the query.
    ''' </summary>
    ''' <remarks>
    ''' Make sure this test exercises every query component that can render using QueryOptions.
    ''' </remarks>
    <TestMethod()> _
    Public Sub QueryOptions_NonDefault()

        Dim Qry As New QBuilder.SelectQuery

        Qry.TableName = "test"
        Qry.Fields.Add("a")
        Qry.Fields.Add("b")
        Qry.Fields.Add("c")

        Qry.AddCriteria(New QBuilder.Criteria("a", QBuilder.Operation.EqualTo, "@Param1"))
        Qry.AddCriteria(New QBuilder.Criteria("b", QBuilder.Operation.EqualTo, "Value"))
        Qry.AddCriteria(New QBuilder.BetweenCriteria("c", "56", "@Param2"))

        Dim subquery As New QBuilder.SelectQuery
        subquery.TableName = "SubTable"
        subquery.Fields.Add("count(*)")
        subquery.AddCriteria(New QBuilder.Criteria("Sub1", QBuilder.Operation.EqualTo, "15"))

        Qry.AddCriteria(New QBuilder.Criteria(subquery, QBuilder.Operation.EqualTo, "2"))
        Qry.AddCriteria(New InCriteria("Field1", New List(Of String)(New String() {"A", "B", "C"})))

        Dim Options As New QueryOptions
        Options.CompressSpaces = True
        Dim expected As String = "SELECT a,b,c FROM test WHERE a=@Param1 AND b='Value' AND c BETWEEN 56 AND @Param2 AND (SELECT count(*) FROM SubTable WHERE Sub1=15)=2 AND Field1 IN ('A','B','C')"
        Dim actual As String = Qry.GetSQLText(Options)

        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()>
    Public Sub QueryOptions_CompressedSpaces()

        Dim Qry As New QBuilder.SelectQuery

        Qry.TableName = "test"
        Qry.Fields.Add("a")
        Qry.Fields.Add("b")
        Qry.Fields.Add("c")

        Dim Options As New QueryOptions
        Options.CompressSpaces = True
        Dim expected As String = "SELECT a,b,c FROM test"
        Dim actual As String = Qry.GetSQLText(Options)
        Assert.AreEqual(expected, actual)

        Dim Criteria As New InCriteria("Field1", New List(Of String)(New String() {"A", "B", "C"}))
        expected = "Field1 IN ('A','B','C')"
        actual = Criteria.GetText(Options)
        Assert.AreEqual(expected, actual)
    End Sub

    ''' <summary>
    ''' Makes sure that a CriteriaGroup always wraps it's self in parens if the QueryOptions tell it to, regardless of sibling count.
    ''' </summary>
    ''' <remarks></remarks>
    <TestMethod()>
    Public Sub QueryOptions_AlwaysWrapGroups()

        Dim Options As New QueryOptions
        Options.AlwaysWrapGroups = True

        Dim target As New SelectQuery()
        target.Fields.Add("A")
        target.TableName = "SomeTable"

        Dim Group As New CriteriaGroup
        Group.AddCriteria(New Criteria("A", Operation.EqualTo, "1"))

        target.AddCriteria(Group)

        Dim expected As String = "SELECT A FROM SomeTable WHERE (A = 1)"
        Dim actual As String
        actual = target.GetSQLText(Options)
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()>
    Public Sub QueryOptions_Terminiated()
        Dim target As SelectQuery = New SelectQuery("SomeTable")
        target.Fields.Add("A")

        Dim options As New QueryOptions With {.TerminateQuery = True}
        Dim expected As String = "SELECT A FROM SomeTable;"
        Dim actual As String
        actual = target.GetSQLText(options)
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()>
    Public Sub QueryOptions_Unterminiated()
        Dim target As SelectQuery = New SelectQuery("SomeTable")
        target.Fields.Add("A")

        Dim options As New QueryOptions With {.TerminateQuery = False}
        Dim expected As String = "SELECT A FROM SomeTable"
        Dim actual As String
        actual = target.GetSQLText(options)
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()>
    <ExpectedException(GetType(ArgumentException))>
    Public Sub QueryOptions_DatabaseType_Other()
        Dim target As New QueryOptions
        target.DatabaseType = DatabaseType.Access
    End Sub
End Class
